package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    // 员工登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Employee loginEmployee) {// 1.接收请求参数
        // 2.调用service登录
        ResultInfo resultInfo = employeeService.login(loginEmployee.getUsername(), loginEmployee.getPassword());
        // 3.返回结果
        return resultInfo;
    }

    @Autowired
    private HttpSession session;

    // 员工退出
    @PostMapping("/employee/logout")
    public ResultInfo logout() {
        // 1.自毁session
        session.invalidate();
        // 2.返回成功
        return ResultInfo.success();
    }

    // 员工列表
    @GetMapping("/employee/find")
    public ResultInfo find(String name) {// 1.接收参数
        // 2.调用service
        List<Employee> list = employeeService.find(name);
        // 3.返回结果
        return ResultInfo.success(list);
    }

    // 新增员工
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee) {// 1.接收参数
        // 2.调用service
        employeeService.save(employee);
        // 3.返回结果
        return ResultInfo.success();
    }

    // 回显员工
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable Long id) {// 1.接收参数
        // 2.调用service
        Employee employee = employeeService.findById(id);
        // 3.返回结果
        return ResultInfo.success(employee);
    }

    // 修改员工
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee) {// 1.接收参数
        // 2.调用service
        employeeService.update(employee);
        // 3.返回结果
        return ResultInfo.success();
    }
}
